/***************************************************************
 *
 * Filename：FileSendWindow.cpp
 * Abstract：the design of chatting window
 * version ：1.0
 *
 * Copyright (c) 2011 SCUT DT&P FY&XC
 *
***************************************************************/

#include "FileSendWindow.h"

FileSendWindow::FileSendWindow(QHostAddress addr)
{
    TCPSAddr = addr;
    send = false;
    //resize(360,220);
    //setMinimumSize(360,220);
    //setMaximumSize(360,220);
    setWindowTitle(tr("Send File"));

    fileSWD = new QWidget(this);
    fileSWD->resize(550,220);
    Filetext()->setText(tr("please choose the file to send"));
    Filetext()->setAlignment(Qt::AlignCenter);
    openButton = new QPushButton();
    openButton->setText(tr("open"));
    connect(openButton,SIGNAL(clicked()),this,SLOT(slotopenfile()));
    sendButton = new QPushButton();
    sendButton->setText(tr("send"));
    sendButton->setEnabled(false);
    connect(sendButton,SIGNAL(clicked()),this,SLOT(slotsendfile()));
    exitButton = new QPushButton();
    exitButton->setText(tr("exit"));
    connect(exitButton,SIGNAL(clicked()),this,SLOT(slotexit()));

    QHBoxLayout *bottomlayout = new QHBoxLayout();
    bottomlayout->addWidget(openButton);
    bottomlayout->addWidget(sendButton);
    bottomlayout->addWidget(exitButton);
    filewindow()->setVisible(true);
    QVBoxLayout *mainlayout = new QVBoxLayout();
    //mainlayout->addSpacing(10);
    mainlayout->addWidget(filewindow());
    mainlayout->addSpacing(20);
    mainlayout->addLayout(bottomlayout);

    fileWindow->setLayout(mainlayout);
    fileWindow->show();
}

//槽函数，打开对话框，选择要发送的文件
void FileSendWindow::slotopenfile()
{
    fileName = QFileDialog::getOpenFileName(this);
        if(!fileName.isEmpty())
        {
            theFileName = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1);
            qDebug()<<"fileName:"<<fileName<<"in FileSendWindow::slotopenfile";
            qDebug()<<"theFileName:"<<theFileName<<"in FileSendWindow::slotopenfile";
            QString TT = "TO transport the File :" + theFileName;
            filetext()->setText(TT);
            sendButton->setEnabled(true);
            sendButton->show();
        }
}

//退出
void FileSendWindow::slotexit()
{
    if(send == true) {
        emit stopsendfile();
    }
    close();
}

//选择好文件后，提醒发送文件名给对方
void FileSendWindow::slotsendfile()
{
    filetext()->setText(tr("Waiting for acception~~~~~~~~~~~~"));
    emit sendFileName(theFileName);
    qDebug()<<"fileName:"<<fileName<<"in FileSendWindow::slotopenfile";
    qDebug()<<"TCPSADDr:"<<TCPSAddr<<"in FileSendWindow::slotopenfile";
}

QString FileSendWindow::getfileName()
{
    return (fileName);
};

//更新提示文本内容
void FileSendWindow::setlabeltext(QString txt)
{
    thetext()->setText(txt);
}

//更新进度数据内容
void FileSendWindow::setProgresstext(QString txt)
{
    progressLabel->setText(txt);
}

//更新进度条
void FileSendWindow::setProgressBar(qint64 max,qint64 value)
{
    progressBar->setMaximum(max);
    progressBar->setValue(value);
}

//更新按钮状态
void FileSendWindow::setopenstate(bool flag)
{
    if(flag == false) {
        openButton->setEnabled(false);
        sendButton->setEnabled(false);
    }
    else {
        openButton->setEnabled(true);
        sendButton->setEnabled(false);
    }

}

//send state
void FileSendWindow::setsendstate(bool flag)
{
    send = flag;
}
